﻿using GameConfig;
using System.Collections.Generic;
using TEngine;
using UnityEngine;

namespace GameLogic
{
    public class BulletModule : BehaviourSingleton<BulletModule>
    {
        List<BulletControl> m_Bullets = new List<BulletControl>();

        List<BulletControl> m_RemoveList = new List<BulletControl>();

        public BulletControl CreateBullet(WeaponData weaponData, BEPUutilities.Vector3 pos, Influence influence)
        {
            var bullet = BulletControl.Create(weaponData, pos, influence);
            bullet.OnExplotionCallback = OnBulletExplotion;
            m_Bullets.Add(bullet);  

            for(var i =0; i<m_Bullets.Count; ++i)
            {
                if(m_Bullets[i].Influence == influence)
                {
                    m_Bullets[i].Collider.SetSpecificRole(bullet.Collider, BEPUphysics.CollisionRuleManagement.CollisionRule.NoBroadPhase);
                    //bullet.Collider.SetSpecificRole(m_Bullets[i].Collider, BEPUphysics.CollisionRuleManagement.CollisionRule.NoBroadPhase);
                }
            }
            return bullet;
        }

        public void DoLogic()
        {
            foreach (var i in m_Bullets)
            {
                i.DoLogic();
            }
            foreach (var i in m_RemoveList)
            {
                m_Bullets.Remove(i);
            }
            m_RemoveList.Clear();
        }
        public void OnBulletExplotion(BulletControl bullet)
        {
            m_RemoveList.Add(bullet);
        }
    }
}
